<template>
  <div class="app-container org">
    <el-card>
      <el-row>
        <el-col :span="24">
          <p class="pinformation">搜索条件</p>
        </el-col>
      </el-row>
      <el-form ref="queryRef" label-width="100" :model="queryParams">
        <el-row>
          <el-col :span="5">
            <el-form-item label="药品名称：" prop="drugName" label-width="80">
              <el-input
                clearable
                maxlength="100"
                placeholder="请输入药品名称"
                v-model="queryParams.drugName"
                @keyup.enter="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item
              label="质量等级："
              prop="qualityLevel"
              label-width="80"
            >
              <el-select
                placeholder="请选择"
                clearable
                v-model="queryParams.qualityLevel"
                filterable
              >
                <el-option
                  v-for="dict in  proxy.addListNoLimit(sys_drug_quality_level)"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="备案证明编号：" prop="code" label-width="110">
              <el-input
                clearable
                @keyup.enter="handleQuery"
                maxlength="100"
                placeholder="请输入"
                v-model="queryParams.code"
              />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item
              label="销售单位："
              prop="saleFactory"
              label-width="80"
            >
              <el-input
                clearable
                @keyup.enter="handleQuery"
                maxlength="100"
                placeholder="请输入销售单位"
                v-model="queryParams.saleFactory"
              />
            </el-form-item>
          </el-col>
          <el-col :span="3">
            <el-form-item label="状态：" prop="stepId" label-width="50">
              <el-select
                placeholder="请选择"
                clearable
                v-model="queryParams.stepId"
                filterable
              >
                <el-option
                  v-for="dict in proxy.addListNoLimit(sys_drug_step)"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="类型：" prop="drugType" label-width="50">
              <el-select
                placeholder="请选择"
                clearable
                v-model="queryParams.drugType"
                filterable
              >
                <el-option
                  v-for="dict in proxy.addListNoLimit(sys_drug_type)"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-row justify="end">
              <el-button type="success" @click="handleQuery">搜索</el-button>
              <el-button class="resetBtn" @click="resetQuery">重置</el-button>
            </el-row>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="5">
            <el-form-item
              label="领用人："
              prop="createUserId"
              label-width="80"
            >
              <el-select clearable filterable v-model="queryParams.createUserId" style="width: 100%">
                <el-option v-for="item in proxy.addListNoLimit(userList, 'userId', 'nickName')" :key="item.userId" :value="item.userId" :label="item.nickName"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item
              label="领用日期："
              prop="expirationType"
              label-width="80"
            >
              <el-date-picker
                v-model="dateRange"
                value-format="YYYY-MM-DD"
                type="daterange"
                :clearable="false"
                :editable="false"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              ></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-row>
          <p class="pinformation" style="margin-top: 4.5px;">化学药品领用列表</p>
          <p class="totalStyle" style="margin-top: 2px;">共 {{ total }} 条数据</p>
          <!-- <el-button type="success" size="small" style="margin-left: 15px;">出入库记录</el-button>
          <el-button type="success" size="small">领用人2批量签字记录</el-button>
          <el-button type="success" size="small">保管人1批量签字记录</el-button>
          <el-button type="success" size="small">保管人2批量签字记录</el-button> -->
        <el-table
          ref="mainTableRef"
          :data="useTable"
          style="margin-top: 5px"
          @selection-change="handleSelChange"
        >
        <!-- <el-table-column type="selection" width="55" align="center" /> -->
          <el-table-column
            label="唯一编号"
            prop="drug.drugCode"
            align="center"
          />
          <el-table-column
            label="化学药品名称"
            prop="drug.drugName"
            align="center"
          />
          <el-table-column label="类型" prop="drug.drugType" align="center">
            <template #default="scope">
              <dict-tag :options="sys_drug_type" :value="scope.row.drug.drugType" />
            </template>
          </el-table-column>
          <el-table-column
            label="质量等级"
            prop="drug.qualityLevel"
            align="center"
          >
          <template #default="scope">
              <dict-tag :options="sys_drug_quality_level" :value="scope.row.drug.qualityLevel" />
            </template>
          </el-table-column>
          <el-table-column label="销售单位" prop="drug.saleFactory" align="center" />
          <el-table-column label="备案证明编号" prop="drug.code" align="center" />
          <el-table-column label="规格" prop="drug.totalGg" align="center">
            <template #default="scope">
            {{  scope.row.drug.totalGg }}
            <span
              v-html="
                transformation(scope.row.drug.ggUnitName)
              "
            ></span>
          </template>
        </el-table-column>
          <el-table-column label="领用量" prop="outCount" align="center" />
          <el-table-column label="领用时间" prop="outDate" align="center" />
          <el-table-column label="领用人1" prop="createUserId" align="center">
            <template #default="scope">
              {{
                proxy.getItemByArray(
                  userList,
                  "userId",
                  scope.row.createUserId,
                  "nickName"
                )
              }}
            </template>
          </el-table-column>
          <el-table-column label="领用人2" prop="createUser2Id" align="center">
            <template #default="scope">
              {{
                proxy.getItemByArray(
                  userList,
                  "userId",
                  scope.row.createUser2Id,
                  "nickName"
                )
              }}
            </template>
          </el-table-column>
          <!-- <el-table-column label="状态" prop="stepId" align="center">
            <template #default="scope">
              <dict-tag :options="sys_drug_step" :value="scope.row.stepId" />
            </template>
          </el-table-column> -->
          <!-- <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
            width="270"
          >
            <template #default="scope">
              <el-button
                link
                type="success"
                @click="useHcyp(scope.row.drugId)"
                >领用2签字</el-button
              >
              <el-button
                link
                type="success"
                @click="useHcyp(scope.row.drugId)"
                >保管1签字</el-button
              >
              <el-button
                link
                type="success"
                @click="useHcyp(scope.row.drugId)"
                >保管2签字</el-button
              >
            </template>
          </el-table-column> -->
        </el-table>
        <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
        />
      </el-row>
    </el-card>
  </div>
</template>

<script setup name="BzwzRecord">
import { listUseList } from "@/api/resource/hxyp";
import { listUser } from "@/api/contract/check";

const { proxy } = getCurrentInstance();

const { sys_drug_type, sys_drug_quality_level, sys_drug_step } = proxy.useDict(
  "sys_drug_type",
  "sys_drug_quality_level",
  "sys_drug_step"
);

const useTable = ref([]);
const multiplexTable = ref([]);
const data = reactive({
  queryParams: {
    pageNum: 1, // 当前页数
    pageSize: 10, // 当前页数据条数
    qualityLevel: -1,
    stepId: -1,
    drugType: -1,
    createUserId: -1
  },
});

const { queryParams } = toRefs(data);

const dateRange = ref([]);

const heightParams = ref([{ tableName: "main", bottomHeight: 21 }]);

function getTableHeight() {
  proxy.adaptiveHeight(proxy, heightParams.value);
}
onMounted(() => {
  nextTick(() => {
    getTableHeight();
    window.addEventListener("resize", getTableHeight);
  })
})
onUpdated(() => {
  nextTick(function () {
    getTableHeight();
  });
});
onBeforeUnmount(() => {
  window.removeEventListener("resize", getTableHeight);
});

const total = ref(0);
function getList() {
  let temp = {};
  debugger;
  temp = {
    "drug.drugName": queryParams.value.drugName,
    "drug.qualityLevel": queryParams.value.qualityLevel,
    "drug.code": queryParams.value.code,
    "drug.saleFactory": queryParams.value.saleFactory,
    "drug.drugType": queryParams.value.drugType,
    "id": queryParams.value.createUserId,
    pageSize: queryParams.value.pageSize,
    pageNum: queryParams.value.pageNum,
  };
  listUseList(proxy.addDateRange(temp, dateRange.value)).then((res) => {
    useTable.value = res.rows;
    total.value = res.total;
  });
}

getList();

function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

function resetQuery() {
  proxy.resetForm("queryRef");
  dateRange.value = [];
  handleQuery();
}

function handleSelChange(selection) {
  multiplexTable.value = selection;
}

watch(
  () => total.value,
  () => {
    if (total.value) {
      heightParams.value = [{ tableName: "main", bottomHeight: 73 }];
    } else {
      heightParams.value = [{ tableName: "main", bottomHeight: 21 }];
    }
    getTableHeight();
  }
);

function transformation(row) {
  const a = proxy.getSupAndSub(row);
  return a;
}

const userList = ref([]);
listUser().then((res) => {
  userList.value = res.data;
});

</script>

<style lang="scss" scoped>
.el-radio {
  margin-right: 5px;
}
</style>
